import { useMemo } from 'react';
import { fetchUserCateInfo } from '@/service/personnelEntry';
import { injectEnumByKey } from '@/utils/form/injectCatelog';
import { ProColumnType } from '@ant-design/pro-table';
import { useRequest } from 'ahooks';
import injectCascader from '../../../utils/form/injectCascader';

export default function useAutoInjectCateInfo<T>(columns: ProColumnType<T>[]) {
    const { loading, data } = useRequest(
        async () => {
            const res = await fetchUserCateInfo();

            if (res.code !== 200) {
                console.error(res.msg || '服务器出现错误');
                return;
            }

            return res.data;
        },
        { manual: false },
    );

    const afterColumns = useMemo(() => {
        const cols = injectCascader(
            injectEnumByKey<T, string>(columns, data, [
                ['corps_id', 'corps'],
                'no_type',
                'serve_status',
            ]),
            data,
            [['army_id', 'army']],
        );
        return cols;
    }, [data, columns]);

    return {
        loading,
        columns: afterColumns,
        data,
    };
}
